
import { Logger } from "../../../engine/Logger";
import { SpriteFrameComponent } from "../../../engine/SpriteFrameComponent";
import ViewBase from "../../../engine/ViewBase";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubMemberList extends ViewBase {

    private txtPage: cc.Label;
    private page_size: number = 5;
    private cur_page: number = 1;
    private totalPage: number = 1
    private totalNum: number = 0;
    private onlineNum: number = 0
    private dataList: Array<object> = []

    private defaultItemNode: cc.Node;
    private txtOnlineNum: cc.Label;

    onEnable(): void {
        super.onEnable()
        this.cur_page = 1
        this.defaultItemNode = this.findChild(this.node, "meberScrollView/view/content/itemNode")
        this.defaultItemNode.active = false;
        this.txtOnlineNum = this.getChild(this.node, "txtOnlineNum", cc.Label);
        this.txtPage = this.getChild(this.node, "txtPage", cc.Label);
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        UIHelper.onAddEvent(btnPre, () => {
            if (this.cur_page > 1) {
                this.cur_page--;
                this.getClubMember();
            }
        })
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(btnNext, () => {
            if (this.cur_page < this.totalPage) {
                this.cur_page++
                this.getClubMember();
            }
        })

        this.getClubMember()
        this.refresh()
    }


    private getClubMember() {
        ClubSocketClient.req_club_member_list(UserModel.instance.nowClub.clubid, this.page_size, this.cur_page, (data: object) => {
            this.totalNum = data["totalNum"]
            this.onlineNum = data["onlineNum"]
            this.dataList = data["members"]
            let groupList: Array<object> = data["groupList"]
            Logger.log("ViewClubMemberList==========", data)
            for (let i = 0; i < this.dataList.length; i++) {
                let obj: object = this.dataList[i]
                let userid: number = obj["userid"]
                let myUserid: number = UserModel.instance.userId
                if(userid == myUserid){
                    obj["cangroup"] = "自己"
                }else{
                    obj["cangroup"] = "允许"
                    for (let j = 0; j < groupList.length; j++) {
                        let userids: Array<number> = groupList[j]["userids"]
                        if (userids && userids.includes(userid) && userids.includes(myUserid)) {
                            obj["cangroup"] = "不允许"
                        }
                    }
                }
            }
            this.dataList.sort((a: object, b: object) => {
                if (a["actor"] == ClubConfig.Club_Actor_Type.owner) {
                    return -1
                }
                else if (b["actor"] == ClubConfig.Club_Actor_Type.owner) {
                    return 1
                }
                return b["actor"] - a["actor"]
            })
            this.refresh();
        })
    }


    private refresh(showData: object = null) {
        this.totalPage = this.totalNum % this.page_size == 0 ? this.totalNum / this.page_size : Math.floor(this.totalNum / this.page_size) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        this.txtPage.string = this.cur_page + "/" + this.totalPage;
        this.txtOnlineNum.string = this.onlineNum + "人"
        this.hideAllChildren(this.defaultItemNode.parent)

        for (let i = 0; i < this.dataList.length; i++) {
            let item: cc.Node = this.defaultItemNode.parent.children[i]
            if (!item) {
                item = cc.instantiate(this.defaultItemNode)
                item.parent = this.defaultItemNode.parent;
            }
            let data: object = this.dataList[i]
            this.initOneItem(data, item)
        }
    }

    private initOneItem(data: object, item: cc.Node) {
        item.active = true;
        let txtName: cc.Label = this.getChild(item, "txtName", cc.Label);
        txtName.string = data["nick_name"]
        let txtId: cc.Label = this.getChild(item, "txtId", cc.Label)
        txtId.string = data["userid"]
        let txtGameTimes: cc.Label = this.getChild(item, "txtGameTimes", cc.Label)
        txtGameTimes.string = data["gametimes"] + ""
        let txtVip: cc.Label = this.getChild(item, "txtVip", cc.Label)
        if (data["invite"]) {
            txtVip.string = data["invite"]
        } else {
            txtVip.string = "无VIP码"
        }
        let txtGroup: cc.Label = this.getChild(item, "txtGroup", cc.Label)
        txtGroup.string = data["cangroup"]
        let zaixian:cc.Sprite = this.getChild(item, "zaixian", cc.Sprite)
        if(data["isOnline"]){
            zaixian.spriteFrame = zaixian.getComponent(SpriteFrameComponent).spriteFrameList[0]
        }else{
            zaixian.spriteFrame = zaixian.getComponent(SpriteFrameComponent).spriteFrameList[1]
        }
        let icon_admin: cc.Sprite = this.getChild(item, "icon_admin", cc.Sprite)
        icon_admin.node.active = false
        if (data["actor"] == ClubConfig.Club_Actor_Type.owner) {
            icon_admin.node.active = true
            icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[0]
        } else if (data["actor"] == ClubConfig.Club_Actor_Type.administrator) {
            icon_admin.node.active = true
            icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[1]
        }else{
            icon_admin.node.active = true
            icon_admin.spriteFrame = icon_admin.getComponent(SpriteFrameComponent).spriteFrameList[2]
        }
    }
}
